package com.zbj.homemade.mybatis;

import com.zbj.homemade.mybatis.conf.Configuration;
import com.zbj.homemade.mybatis.conf.MappedStatement;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: principle
 * @description:
 * @author: Yinn
 * @create: 2019-01-11 17:02
 **/
public class SimpleExecutor implements Executor {


    @Override
    public int update(MappedStatement ms, Object[] parameter) throws SQLException {
        //和select同理实现
        return 0;
    }

    @Override
    public <E> List<E> query(MappedStatement ms, Object[] parameter) throws SQLException {

        List<E> results = new ArrayList<>();

        Configuration configuration = ms.getConfiguration();
        String user = configuration.getUsername();
        String password = configuration.getPassword();
        String url = configuration.getUrl();
        String driver = configuration.getDriver();
        Connection con = null; //一个封装了TCP长连接 的 数据库长连接对象
        Statement stmt = null; //一个封装和管理SQL语句的java对象
        ResultSet rSet = null; //一个封装了数据对象 的 无序集合对象
        try {
            Class.forName(driver); //加载数据库驱动到JVM中，并实例化为Driver对象
            con = DriverManager.getConnection(url, user, password); //建立TCP数据库长连接，获取Connection对象
            stmt = con.createStatement(); //获取SQL管理对象Statement
            rSet = stmt.executeQuery(ms.fillParameter(parameter));
            int col = rSet.getMetaData().getColumnCount();

            Class<?> resultType = ms.getResultType();

            while (rSet.next()) {

                E obj = (E) resultType.newInstance();

                for (int i = 1; i <= col; i++) {
                    String colName = rSet.getMetaData().getColumnName(i);
                    Object val = rSet.getObject(colName);

                    Field field = resultType.getDeclaredField(colName);

                    if (field == null)
                        throw new RuntimeException("实体类" + resultType.getName() + "中不存在" + colName + "字段，请仔细检查!");

                    field.setAccessible(true);

                    field.set(obj, val);

                    results.add(obj);
                }

            }
        } catch (SQLException | ClassNotFoundException | IllegalAccessException | InstantiationException | NoSuchFieldException e) {
            e.printStackTrace();
        } finally {
            if (rSet != null) rSet.close();
            if (con != null) con.close();
            if (stmt != null) stmt.close();
        }

        return results;
    }

    public static void main(String[] arg) throws Exception {
        String user = "root";
        String password = "yinnan";
        String url = "jdbc:mysql://localhost:3306/lagou_job?useUnicode=true&amp;characterEncoding=UTF8";
        String driver = "com.mysql.jdbc.Driver";
        Connection con = null; //一个封装了TCP长连接 的 数据库长连接对象
        Statement stmt = null; //一个封装和管理SQL语句的java对象
        ResultSet rSet = null; //一个封装了数据对象 的 无序集合对象
        try {
            Class.forName(driver); //加载数据库驱动到JVM中，并实例化为Driver对象
            con = DriverManager.getConnection(url, user, password); //建立TCP数据库长连接，获取Connection对象
            stmt = con.createStatement(); //获取SQL管理对象Statement
            rSet = stmt.executeQuery("select * from job");
            int col = rSet.getMetaData().getColumnCount();

            while (rSet.next()) {
                for (int i = 1; i <= col; i++) {
                    String colName = rSet.getMetaData().getColumnName(i);
                    Object val = rSet.getObject(colName);
                }

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (rSet != null) rSet.close();
            if (con != null) con.close();
            if (stmt != null) stmt.close();
        }
    }
}
